﻿using Spire.Pdf;
using Spire.Pdf.HtmlConverter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HtmlToPdfTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请输入网址！");
                textBox1.Focus();
                return;
            }
            try
            {
                PdfDocument doc = new PdfDocument();
                PdfPageSettings setting = new PdfPageSettings
                {
                    Size = new SizeF(1000, 1000),
                    Margins = new Spire.Pdf.Graphics.PdfMargins(20)
                };
                PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat
                {
                    IsWaiting = true
                };
                Thread thread = new Thread(() =>
                {
                    doc.LoadFromHTML(textBox1.Text, false, false, false, setting, htmlLayoutFormat);
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
                //Save pdf file.
                doc.SaveToFile("output-wiki.pdf");
                doc.Close();
                //Launching the Pdf file.
                System.Diagnostics.Process.Start("output-wiki.pdf");
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现异常：" + ex);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请输入需要转换的内容！");
                textBox2.Focus();
                return;
            }
            try
            {
                PdfDocument doc = new PdfDocument();
                PdfPageSettings setting = new PdfPageSettings
                {
                    Size = new SizeF(1000, 1000),
                    Margins = new Spire.Pdf.Graphics.PdfMargins(20)
                };
                PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat
                {
                    IsWaiting = true
                };
                Thread thread = new Thread(() =>
                {
                    doc.LoadFromHTML(textBox2.Text, true, setting, htmlLayoutFormat);
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
                //Save pdf file.
                doc.SaveToFile("output-wiki.pdf");
                doc.Close();
                //Launching the Pdf file.
                System.Diagnostics.Process.Start("output-wiki.pdf");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
